/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Window.java
 *
 * Created on Oct 1, 2009, 11:21:40 AM
 */
package onedimensionautomata;

import java.awt.Point;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author x00875
 */
public class Window extends javax.swing.JFrame {

    /** This stores the initial world used for the simulation. */
    World initialWorld;
    /** Stores the history of the simulation. */
    ArrayList<World> history;
    /** Rules used in this game. */
    RuleInterface RULES = Rules.SIS;

    /**
     * This initializes the initial world, with the default topology. Randomizes
     * the initial states. Also resets the topology view.
     */
    void initialize_world_and_reset_topology() {
        int s = (Integer) sizeSpinner.getValue();
        initialWorld = new World(s, 4);
        Main.randomizeWorld(initialWorld);
        initialWorld.topology = new WorldConnectionMatrix(initialWorld);
        adjacencyMatrix.setData(initialWorld.topology.getData());
    }

    /**
     * Uses the current world to set up and run the simulation, and updates
     * the resulting displays.
     */
    void run_simulation_and_update_display() {
        // run the simulation
        int g = (Integer) generationSpinner.getValue();
        history = Main.run(initialWorld, g, RULES);

        // update main vis window
        int nstates = initialWorld.nstates;
        worldView1.setData(history);

        // update table with counts
        Integer[][] countHistory = new Integer[g][nstates];
        for (int i = 0; i < g; i++) {
            countHistory[i] = history.get(i).countStates();
        }
        String[] colNames = new String[nstates];
        for (int i = 0; i < colNames.length; i++) {
            colNames[i] = "State " + i;
        }
        jTable1.setModel(new DefaultTableModel(countHistory, colNames));

        // draw the updated results
        repaint();
    }

    /** Creates new form Window */
    public Window() {
        initComponents();
        paletteCombo.setModel(new DefaultComboBoxModel(StatePalette.values()));
        rulesComboBox.setModel(new DefaultComboBoxModel(Rules.values()));

        initialize_world_and_reset_topology();
        run_simulation_and_update_display();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        resetButton = new javax.swing.JButton();
        randomizeButton = new javax.swing.JButton();
        generationSpinner = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        sizeSpinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        symmetricCheckBox = new javax.swing.JCheckBox();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        rulesComboBox = new javax.swing.JComboBox();
        paletteCombo = new javax.swing.JComboBox();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        worldView1 = new onedimensionautomata.WorldView();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        adjacencyMatrix = new onedimensionautomata.WorldView();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("1D Cellular Automata");

        jToolBar1.setRollover(true);
        jToolBar1.setMaximumSize(new java.awt.Dimension(65655, 23));

        resetButton.setText("Reset");
        resetButton.setFocusable(false);
        resetButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        resetButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(resetButton);

        randomizeButton.setText("Randomize");
        randomizeButton.setFocusable(false);
        randomizeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        randomizeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        randomizeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randomizeButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(randomizeButton);

        generationSpinner.setValue(5);
        generationSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                generationSpinnerStateChanged(evt);
            }
        });
        jToolBar1.add(generationSpinner);

        jLabel1.setText("Generations");
        jToolBar1.add(jLabel1);
        jToolBar1.add(jSeparator1);

        sizeSpinner.setValue(20);
        sizeSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sizeSpinnerStateChanged(evt);
            }
        });
        jToolBar1.add(sizeSpinner);

        jLabel2.setText("Size");
        jToolBar1.add(jLabel2);
        jToolBar1.add(jSeparator2);

        symmetricCheckBox.setSelected(true);
        symmetricCheckBox.setText("Symmetric");
        symmetricCheckBox.setFocusable(false);
        symmetricCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        symmetricCheckBox.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(symmetricCheckBox);
        jToolBar1.add(jSeparator3);

        rulesComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        rulesComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rulesComboBoxActionPerformed(evt);
            }
        });
        jToolBar1.add(rulesComboBox);

        paletteCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        paletteCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paletteComboActionPerformed(evt);
            }
        });
        jToolBar1.add(paletteCombo);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        jSplitPane1.setDividerLocation(400);
        jSplitPane1.setResizeWeight(0.75);

        worldView1.setPreferredSize(new java.awt.Dimension(300, 300));
        worldView1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                worldView1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(worldView1);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        jTabbedPane1.addTab("State Count Table", jScrollPane2);

        adjacencyMatrix.setPreferredSize(new java.awt.Dimension(200, 200));
        adjacencyMatrix.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                adjacencyMatrixMouseClicked(evt);
            }
        });
        jTabbedPane1.addTab("Topology / Adjacency Matrix", adjacencyMatrix);

        jSplitPane1.setRightComponent(jTabbedPane1);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        initialize_world_and_reset_topology();
        run_simulation_and_update_display();
    }//GEN-LAST:event_resetButtonActionPerformed

    private void worldView1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_worldView1MouseClicked
        Point click = worldView1.rowCol;
        if (click.y == 0) {
            // cycles state at specified location in initial world
            initialWorld.setWorldValue(click.x,
                    (initialWorld.getWorld()[click.x] + 1) % initialWorld.nstates);
            run_simulation_and_update_display();
        }
    }//GEN-LAST:event_worldView1MouseClicked

    private void adjacencyMatrixMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_adjacencyMatrixMouseClicked
        // called when the adjacency matrix visualization is clicked
        Point click = adjacencyMatrix.rowCol;
        boolean symmetric = symmetricCheckBox.isSelected();
        initialWorld.topology.toggleConnection(click.y, click.x, symmetric);
        adjacencyMatrix.repaint();
        run_simulation_and_update_display();
    }//GEN-LAST:event_adjacencyMatrixMouseClicked

    private void randomizeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomizeButtonActionPerformed
        Main.randomizeWorld(initialWorld);
        run_simulation_and_update_display();
    }//GEN-LAST:event_randomizeButtonActionPerformed

    private void generationSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_generationSpinnerStateChanged
        run_simulation_and_update_display();
    }//GEN-LAST:event_generationSpinnerStateChanged

    private void sizeSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sizeSpinnerStateChanged
        initialize_world_and_reset_topology();
        run_simulation_and_update_display();
    }//GEN-LAST:event_sizeSpinnerStateChanged

    private void paletteComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paletteComboActionPerformed
        worldView1.palette = StatePalette.valueOf(paletteCombo.getSelectedItem().toString());
        repaint();
    }//GEN-LAST:event_paletteComboActionPerformed

    private void rulesComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rulesComboBoxActionPerformed
        this.RULES = Rules.valueOf(rulesComboBox.getSelectedItem().toString());
        this.run_simulation_and_update_display();
}//GEN-LAST:event_rulesComboBoxActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Window().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private onedimensionautomata.WorldView adjacencyMatrix;
    private javax.swing.JSpinner generationSpinner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JComboBox paletteCombo;
    private javax.swing.JButton randomizeButton;
    private javax.swing.JButton resetButton;
    private javax.swing.JComboBox rulesComboBox;
    private javax.swing.JSpinner sizeSpinner;
    private javax.swing.JCheckBox symmetricCheckBox;
    private onedimensionautomata.WorldView worldView1;
    // End of variables declaration//GEN-END:variables
}
